package com.koron.sinoprobe.web.plan.excel;

import cn.hutool.core.map.BiMap;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.AutoConverter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.koron.common.core.business.datadict.utils.DataDictTool;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author jzp
 * @since 2022/5/16
 */
public class SpPlanStringStringConvertor extends AutoConverter {

    /**
     * {
     *     字段名：{
     *         数据库值：前端显示值
     *     }
     * }
     */
    private final Map<String, BiMap<String, String>> fieldMap = new HashMap<>();
    private final String title = "$title$";

    /**
     * 初始化字典数据
     */
    public SpPlanStringStringConvertor(){
        // 业务类别 bizCategory
        BiMap<String, String> bizCategoryMap = new BiMap<>(DataDictTool.getDataDictByCode("sinoProbeManagement", "biz_category"));
        bizCategoryMap.put(title, "业务类别");

        // 项目类别 projectType
        BiMap<String, String> projectTypeMap = new BiMap<>(DataDictTool.getDataDictByCode("sinoProbeManagement", "sp_project_category"));
        projectTypeMap.put(title, "项目类别");

        // 专业 major
        BiMap<String, String> majorMap = new BiMap<>(DataDictTool.getDataDictByCode("sinoProbeManagement", "sp_major_category"));
        majorMap.put(title, "专业");

        // 预算类型 budgetType
        BiMap<String, String> budgetTypeMap = new BiMap<>(DataDictTool.getDataDictByCode("sinoProbeManagement", "budget_type"));
        budgetTypeMap.put(title, "预算类型");

        // 是否团队 是否计划内 isTeam和isExpected
        BiMap<String, String> boolMap = new BiMap<>(new HashMap<>());
        boolMap.put("0", "否");
        boolMap.put("1", "是");
        boolMap.put(title, "布尔值");

        // 重要程度 important_level
        BiMap<String, String> importantLevelMap = new BiMap<>(DataDictTool.getDataDictByCode("sinoProbeManagement", "important_level "));
        bizCategoryMap.put(title, "重要程度");

        // 字段和数据字典对应
        fieldMap.put("bizCategory", bizCategoryMap);
        fieldMap.put("projectType", projectTypeMap);
        fieldMap.put("major", majorMap);
        fieldMap.put("budgetType", budgetTypeMap);
        fieldMap.put("isTeam", boolMap);
        fieldMap.put("isExpected", boolMap);
        fieldMap.put("importantLevel", importantLevelMap);
    }

    @Override
    public Class<?> supportJavaTypeKey() {
        return Object.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                    GlobalConfiguration globalConfiguration) {
        String key = fieldMap.get(contentProperty.getField().getName()).getKey(cellData.getStringValue());
        if (StrUtil.isNotEmpty(cellData.getStringValue()) && StrUtil.isEmpty(key)) {
            throw new IllegalArgumentException("找不到" + cellData.getStringValue() + "对应的" + fieldMap.get(contentProperty.getField().getName()).get(title));
        }
        Class<?> type = contentProperty.getField().getType();
        if (StrUtil.isEmpty(key)) {
            return null;
        }
        if (type == Integer.class) {
            return Integer.parseInt(key);
        }
        return key;
    }

    @Override
    public WriteCellData<String> convertToExcelData(Object value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        return new WriteCellData<>(fieldMap.get(contentProperty.getField().getName()).get(Objects.toString(value, "")));
    }

}
